package com.wbc.smartHome.web;

import com.github.pagehelper.PageInfo;
import com.wbc.smartHome.model.CommodityType;
import com.wbc.smartHome.model.Result;
import com.wbc.smartHome.service.CommodityTypeService;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

import java.io.IOException;


@RestController
@RequestMapping(path = "/api/commodityTypeCtl")
/*热门商品存取*/
@Api(tags = "商品类型模块")
public class CommodityTypeController {


    @Autowired
    private CommodityTypeService commodityTypeService;

    @ApiOperation("获取全部商品类型")
    @ApiResponses({
            @ApiResponse(code = 200 , message = "OK"),
            @ApiResponse(code = 500 , message = "系统异常")
    })
    @RequestMapping(path = "getCommodityTypeList")
    public Result getCommodityTypeList(@RequestBody CommodityType commodityType) {
        PageInfo<CommodityType> commodityTypeList = commodityTypeService.getCommodityTypeList(commodityType);
        return new Result(200, "OK", commodityTypeList);
    }

    @ApiOperation("删除商品类型")
    @ApiResponses({
            @ApiResponse(code = 200 , message = "删除成功"),
            @ApiResponse(code = 500 , message = "系统异常")
    })
    @GetMapping(path = "delete")
    public Result delete(int id){
        commodityTypeService.delete(id);
        Result result = new Result(200, "删除成功", null);
        return result;
    }

    @ApiOperation("保存商品类型信息")
    @ApiResponses({
            @ApiResponse(code = 200 , message = "OK"),
            @ApiResponse(code = 500 , message = "系统异常")
    })
    @PostMapping(path = "save")
    public Result save(@RequestBody CommodityType commodityType,@RequestHeader("adminToken") String adminToken) {
        if (commodityType.getId()==null){
            commodityTypeService.setCommodityType(commodityType,adminToken);
        }
        else {
            commodityTypeService.updateCommodityType(commodityType,adminToken);
        }
        return new Result(200,"ok",null);
    }

    @ApiOperation("上传类型封面图")
    @PostMapping("/uploadImg")
    public Result uploadImg(@RequestParam("file") CommonsMultipartFile commonsMultipartFile) throws IOException {
        Result result = commodityTypeService.uploadImg(commonsMultipartFile);
        return  result;
    }

    @ApiOperation("按照id查询商品类型")
    @ApiResponses({
            @ApiResponse(code = 200 , message = "查询成功"),
            @ApiResponse(code = 500 , message = "系统异常")
    })
    @GetMapping(path = "getCommodityTypeById")
    public Result getCommodityTypeById(int id){
        CommodityType commodityType = commodityTypeService.getCommodityTypeById(id);
        Result result = new Result(200, "查询成功", commodityType);
        return result;
    }
}
